<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Provinder extends Admin {

	public function __construct()
    {
        parent::__construct();
		$this->load->model("provinder_model");
    }
	public function index()
	{
        $page = $this->uri->segment(4);
        $page_size = $this->getPageSize;
        $param = array();
		$data = array();
        $page_url = base_url('/cp/provinder/index');
        $total = $this->provinder_model->count_all($param);
        $pagination = $this->get_search_paging($page_url, $total, $page, $page_size );
        $data['records'] = $this->provinder_model->find_all($param,$page, $page_size);
        $data['pagination'] = $pagination;
		$this->layout->view($data);
	}
	public function add(){
        $data=array();
		$this->load->view('cp/provinder/add',$data);

	}
	public function edit($id=null){
		$data['items'] = $this->provinder_model->getSingle($id);
		$this->load->view('cp/provinder/edit',$data);
	}

	public function save(){
		$data = array();
		$this->form_validation->set_rules('name', 'Name', 'required');
		$this->form_validation->set_rules('region', 'region', 'required');
		$this->form_validation->set_rules('orderNo', 'orderNo', 'required');
		$data['flag'] = true;
		if ($this->form_validation->run() == TRUE){
			$obj['name'] = $this->input->post('name');
			$obj['region'] = $this->input->post('region');
			$obj['orderNo'] = $this->input->post('orderNo');
			$obj['alias'] = $this->input->post('alias');
			$objId = $this->provinder_model->insert($obj);
			if($objId) {
				$data['msg'] = "Them moi thanh cong";
			}else {
				$data['msg'] = "Loi khi them moi";
				$data['flag'] = false;
			}

		}else {
			$data['msg'] = "Thong tin sai";
			$data['flag'] = false;
		}
		echo json_encode($data);

		exit();
	}

	public function do_update(){
		$data = array();
		$data['flag'] 	= true;
		$obj['name'] 	= $this->input->post('name');
		$obj['region'] 	= $this->input->post('region');
		$obj['orderNo'] = $this->input->post('orderNo');
		$obj['alias'] = $this->input->post('alias');
		$id 			= $this->input->post('id');
		$objId = $this->provinder_model->update($obj,$id);
		if($objId){
			$data['msg'] = "Them moi thanh cong";
		}else {
			$data['msg'] = "Loi khi them moi";
			$data['flag'] = false;
		}
		echo json_encode($data);
		exit();
	}

	function publish(){
		$data = array();
		$this->load->model("district_model");
		$data['provinder'] = $this->provinder_model->getListAll();
		$html ='<div class="provinder">';
			$html.='<div class="items"><a <?php if($provinder_id==100){ echo "class=\"active\"";}?>  href="<?php echo site_url("Toan-Quoc"); ?>">Toàn Quốc</a></div>';
			$html.='<div class="items"><a <?php if($provinder_id==1){ echo "class=\"active\"";}?> href="<?php echo site_url("Ha-Noi"); ?>">Hà Nội</a></div>';
			$html.='<div class="items"><a <?php if($provinder_id==51){ echo "class=\"active\"";}?> href="<?php echo site_url("TP-HCM"); ?>">TP Hồ Chí Minh</a></div>';
			$html.='<div class="items"><a <?php if($provinder_id==2){ echo "class=\"active\"";}?> href="<?php echo site_url("Hai-Phong"); ?>">Hải Phòng</a></div>';
			$html.='<div class="items"><a <?php if($provinder_id==50){ echo "class=\"active\"";}?>  href="<?php echo site_url("Da-Nang"); ?>">Đà Nẵng</a></div>';
			$html.='<?php if($provinder_id !=1 && $provinder_id !=2 && $provinder_id !=100 && $provinder_id !=51 && $provinder_id !=50 && $provinder_id !=56){ ?>';
			$html.='<div class="items"><a class="active" href="<?php echo site_url("Hai-Phong"); ?>">Hải Phòngs</a></div>';
			$html.='<?php }else { ?>';
			$html.='<div class="items"><a <?php if($provinder_id==56){ echo "class=\"active\"";}?> href="<?php echo site_url("Binh-Duong"); ?>">Bình Dương</a></div>';
			$html.="<?php } ?>";
			$html.='<div class="items no-bg"><a href="javascript:void(0);" class="drop">Chọn Tỉnh/Thành</a>';
			$html.='<div class="showcity" style="display:none;"><div class="boxOvlCity"><div class="contentOvlCity">';
			$html.='<div class="RegionPanel"><div class="Region" style="background:none">Miền Bắc</div>';
			 $i=0;
            foreach($data['provinder'] as $key=>$item){
                if($item['region']==1){
                    $css ="";
                    if($i==0 || $i==1){$css = "itemactive";}
                    $html.= '<div class="item clear"><a href="../admin/'.site_url($item["alias"]).'" class="'.$css.'">'.$item['name'].'</a></div>';
                    $i++;
                }
            }
			$html.="</div>";

			$html.='<div class="RegionPanel"><div class="Region" style="background:none">Miền Trung</div>';
            $j=0;
            foreach($data['provinder'] as $key=>$item){
                if($item['region']==2){
                    $css ="";
                    if($j==0){$css = "itemactive";}
                    $html.='<div class="item clear"><a href="../admin/'.site_url($item["alias"]).'" class="'.$css.'">'.$item['name'].'</a></div>';
                    $j++;
                }
            }
           $html.='</div>';
		   $html.='<div class="RegionPanel"><div class="Region close">Miền Nam</div>';
            $k=0;
            foreach($data['provinder'] as $key=>$item){
                 if($item['region']==3){
                    $css ="";
                    if($k==0){$css = "itemactive";}
                   $html.= '<div class="item clear"><a href="../admin/'.site_url($item["alias"]).'" class="'.$css.'">'.$item['name'].'</a></div>';
                    $k++;
                }
            }
           $html.='</div>';

		$html.='</div></div></div></div></div>';


		//$content = $this->load->view('admin/provinder/publish',$data,TRUE);
		$file = APPPATH."views/_partials/submenu.php";
		@file_put_contents($file, $html);

		$this->output->enable_profiler(FALSE);
        $this->output->set_content_type('application/json')->set_output(json_encode(array('success'=>"Thành công")));
	}

}